package com.lagou.controller;

import com.lagou.pojo.VeriCode;
import com.lagou.service.CodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RequestMapping("code")
@RestController
public class CodeController {

    @Autowired
    private CodeMapper codeMapper;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Integer addCode(@RequestBody VeriCode veriCode){
        return codeMapper.addCode(veriCode);
    }

    @RequestMapping(value = "/create/{email}", method = RequestMethod.GET)
    public String create(@PathVariable("email") String email) {
         VeriCode veriCode = new VeriCode();
         veriCode.setEmail(email);
         String code = String.format(String.valueOf(new Random().nextInt(9999)), "%4d");
         veriCode.setCode(code);
         codeMapper.addCode(veriCode);
         return code;
    }


}
